package com.play.trading_test.config;

import jakarta.annotation.Resource;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Data
@Component
public class CacheUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private CommonUtil commonUtil;

    /**
     * string 操作,最终对象会被序列化为json
     *
     * @param key   key
     * @param value 值
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(CacheConstant.PREFIX + key, value);
    }

    public void set(String key, Object value, int seconds) {
        redisTemplate.opsForValue().set(CacheConstant.PREFIX + key, value, seconds, TimeUnit.SECONDS);
    }

    public Object get(String key) {
        return redisTemplate.opsForValue().get(CacheConstant.PREFIX + key);
    }

    public <T> T getForClass(String key, Class<T> clazz) {
        Object cacheData = redisTemplate.opsForValue().get(CacheConstant.PREFIX + key);
        if (cacheData == null || StringUtils.isBlank(cacheData.toString())) return null;
        try {
            return commonUtil.convertBean(commonUtil.obj2Str(cacheData), clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 得到一个list集合
     *
     * @param key key
     * @param <T> 返回类型
     * @return List<T>
     */
    public <T> List<T> getToList(String key, Class<T> clazz) {
        Object cacheData = redisTemplate.opsForValue().get(CacheConstant.PREFIX + key);
        if (cacheData == null || StringUtils.isBlank(cacheData.toString())) return null;
        try {
            return commonUtil.json2List(commonUtil.obj2Str(cacheData), clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * set操作
     *
     * @param key  key
     * @param data 值
     */
    public void setForSet(String key, Object data) {
        redisTemplate.opsForSet().add(CacheConstant.PREFIX + key, data);
    }

    /**
     * 在set中是否存在
     *
     * @param key  key
     * @param data 值
     * @return Boolean
     */
    public Boolean setContains(String key, Object data) {
        return redisTemplate.opsForSet().isMember(CacheConstant.PREFIX + key, data);
    }


    public void delete(String key) {
        redisTemplate.delete(CacheConstant.PREFIX + key);
    }


    public void zSetAdd(String key, Object value, Integer score) {
        redisTemplate.opsForZSet().add(CacheConstant.PREFIX + key, value, score);
    }

    public Set<Object> zSetRange(String key, long start, long end) {
        return redisTemplate.opsForZSet().range(CacheConstant.PREFIX + key, start, end);
    }

    public Long zSetReverseRank(String key, Object value) {
        return redisTemplate.opsForZSet().reverseRank(CacheConstant.PREFIX + key, value);
    }

    public Double zSetScore(String key, Object value) {
        return redisTemplate.opsForZSet().score(CacheConstant.PREFIX + key, value);
    }

    public void zSetIncrementScore(String key, Object value, Integer incr) {
        redisTemplate.opsForZSet().incrementScore(CacheConstant.PREFIX + key, value, incr);
    }

    public void zSetRemoveRangeByScore(String key, Object... objects) {
        redisTemplate.opsForZSet().remove(CacheConstant.PREFIX + key, objects);
    }


}
